package org.crmsystem.controller;

import java.lang.ProcessBuilder.Redirect;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.model.*;
import org.crmsystem.service.IAppointService;
import org.crmsystem.service.IContactService;
import org.crmsystem.service.IOrgService;
import org.crmsystem.service.IStaffService;
import org.crmsystem.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 预约信息controller
 * 
 * @author 刘鹏博
 *
 */
@Controller
@RequestMapping("/appoint")
public class AppointController {
	private static Logger logger=LogManager.getLogger(AppointController.class);
	/**
	 * 预约IAppointService接口
	 */
	@Autowired
	private IAppointService appoint;
	/**
	 * 机构IOrgService接口
	 */
	@Autowired
	private IOrgService orgservice;
	/**
	 * 联系人service接口
	 */
	@Autowired
	private IContactService contactservice;
	/**
	 * 员工IStaffService接口
	 */
	@Autowired
	private IStaffService staffService;
	@RequestMapping("/add")
	public ModelAndView add(){
		return getList("addAppoint");
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/allAppoint")
	public ModelAndView getAppoint(){

		String type="all";
		String keyword="";	
		ArrayList<Appoint> app=(ArrayList<Appoint>) appoint.getAppointByKeyword(type, keyword);
		ModelAndView mav=new ModelAndView("allAppoint");
		mav.addObject("appoints", app);
		return mav;		
	}

	    /**
	     * 删除预约信息
	     * @param idString
	     * @return
	     */
	    @RequestMapping("/delAppoint")
	    public String deleteApp(String idString) {
			int id = Integer.parseInt(idString);
			appoint.deleteAppoint(id);			
			return "forward:/appoint/allAppoint";
		}
	    /**
	     * 查看预约详情
	     * @param idString
	     * @return
	     */
	    @RequestMapping("/appDetail")
	    public ModelAndView appDetail(String idString){
	    	int id=Integer.parseInt(idString);
	    	Appoint app=appoint.getAppById(id);
	    	app.setTimeStr(DateUtil.formatDate(app.getTime(),"yyyy-MM-dd"));
	    	app.setCreateDateStr(DateUtil.formatDate(app.getCreateDate(), "yyyy-MM-dd"));
	    	ModelAndView mav=new ModelAndView("appDetail");
	    	mav.addObject("app",app);
	    	return mav;
	    	}
	    /**
	     * 新增预约信息
	     * @param app
	     * @param orgIdStr
	     * @param contactIdStr
	     * @param timeStr
	     * @param appointSetIdStr
	     * @param operatorIdStr
	     * @param createDateStr
	     * @return
	     * @throws Exception
	     */
	    @RequestMapping("/addAppoint")
	    public String addAppoint(Appoint app,String orgIdStr,String contactIdStr,String timeStr,String appointSetIdStr
	    		,String operatorIdStr,String createDateStr) throws Exception{	    	
	    	app.setOrgId(Integer.parseInt(orgIdStr));	    	
	    	app.setContactId(Integer.parseInt(contactIdStr));
	    	app.setAppointSetId(Integer.parseInt(appointSetIdStr));
	    	app.setOperatorId(Integer.parseInt(operatorIdStr));
	    	if (timeStr==null||timeStr.equals("")) {
				timeStr=DateUtil.formatDate(new Date(),"yyyy-MM-dd");
			}
	    	app.setTime(DateUtil.formatDate(timeStr, "yyyy-MM-dd"));
	    	if (createDateStr==null||createDateStr.equals("")) {
	    		createDateStr=DateUtil.formatDate(new Date(),"yyyy-MM-dd");
			}
	    	app.setCreateDate(DateUtil.formatDate(createDateStr, "yyyy-MM-dd"));
	    	int flag=appoint.insertAppoint(app);
	    	return "redirect:/appoint/allAppoint";
	    	}
	    /**
	     * 封装多个List
	     * @param pathStr
	     * @return
	     */
	    public ModelAndView getList(String pathStr){
	    	HashMap<String,Object> omap=new HashMap<String, Object>();
	    	omap.put("search","name");
	    	omap.put("searchText", "");
	    	List<OrgInfo> olist=orgservice.selectOrgList(omap);
	    	ArrayList<Contact> clist=contactservice.selectContactList(null);
	    	Map map=new HashMap();
	    	map.put("staff", new Staff());
	    	map.put("pageSize", 100);
	    	map.put("currPage", 1);
			Page page=staffService.getStaff(map);
			List<Object> slist=page.getResult();
	    	
	    	ModelAndView mm=new ModelAndView(pathStr);
	    	mm.addObject("olist", olist);
	    	mm.addObject("clist", clist);
	    	mm.addObject("slist", slist);
	    	return mm;
	    }
	    /**
	     * 精确查询机构，带出机构信息
	     * @param morgId
	     * @return
	     */
	    @RequestMapping("/searchorgname")
	    @ResponseBody
	    public OrgInfo getOrgById(String morgId){
	        int orgId = Integer.parseInt(morgId);
	        OrgInfo orgInfo = orgservice.getOrgById(orgId);
	        return orgInfo;	
	    }
	    /**
	     * 根据id获取要修改的预约信息
	     * @param idStrng
	     * @return
	     */
	    @RequestMapping("/updated")
	    public ModelAndView updateById(String idStrng){
	    	int id=Integer.parseInt(idStrng);
	    	Appoint ap=appoint.getAppById(id);
	    	ap.setTimeStr(DateUtil.formatDate(ap.getTime(), "yyyy-MM-dd"));
	    	ap.setCreateDateStr(DateUtil.formatDate(ap.getCreateDate(), "yyyy-MM-dd"));
	    	Map<String,Object> map=new HashMap<String, Object>();
	    	ModelAndView mm=getList("addAppoint");
	    	mm.addObject("A",ap);
	    	return mm;
	    }
	    /**
	     * 修改预约信息
	     * @param app
	     * @param orgIdStr
	     * @param contactIdStr
	     * @param timeStr
	     * @param appointSetIdStr
	     * @param operatorIdStr
	     * @param createDateStr
	     * @return
	     * @throws Exception
	     */
	    @RequestMapping("/updateAppoint")
	    public String updateAppoint(Appoint app,String orgIdStr,String contactIdStr,String timeStr,String appointSetIdStr
	    		,String operatorIdStr,String createDateStr) throws Exception{
	    	app.setOrgId(Integer.parseInt(orgIdStr));	    	
	    	app.setContactId(Integer.parseInt(contactIdStr));
	    	app.setAppointSetId(Integer.parseInt(appointSetIdStr));
	    	app.setOperatorId(Integer.parseInt(operatorIdStr));
	    	if (timeStr==null||timeStr.equals("")) {
				timeStr=DateUtil.formatDate(new Date(),"yyyy-MM-dd");
			}
	    	app.setTime(DateUtil.formatDate(timeStr, "yyyy-MM-dd"));
	    	if (createDateStr==null||createDateStr.equals("")) {
	    		createDateStr=DateUtil.formatDate(new Date(),"yyyy-MM-dd");
			}
	    	app.setCreateDate(DateUtil.formatDate(createDateStr, "yyyy-MM-dd"));
	    	int flag=appoint.updateAppointById(app);
	    	return "redirect:/appoint/allAppoint";
	    }
}



